package joo.config;

import java.util.HashMap;
import java.util.Map;

import joo.model._MappingKit;
import joo.module.base.IndexController;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;

/**
 * API引导式配?
 */
public class AppConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取�?
		loadPropertyFile("app.properties");
		me.setMainRenderFactory(new BeetlRenderFactory());
		// 获取GroupTemplate ,可以设置共享变量等操�?
		GroupTemplate groupTemplate = BeetlRenderFactory.groupTemplate ;
		Map<String,Object> sharedVars = new HashMap<String,Object>();
		me.setBaseViewPath("/WEB-INF/view");
		sharedVars.put("appName", getProperty("appName"));
		groupTemplate.setSharedVars(sharedVars);
		me.setDevMode(getPropertyToBoolean("devMode", false));
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		//me.add("/admin", AdminController.class, "/admin");	// 第三个参数为该Controller的视图存放路�?
		me.add("/", IndexController.class);
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		_MappingKit.mapping(arp);
		me.add(arp);
	}
	
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		//me.add(new LoginCheckInterceptor());
	}

	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler());
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项�?
	 * 运行�? main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不�?定要放于�?
	 */
	public static void main(String[] args) {
		JFinal.start("WebContent", 80, "/", 5);
	}
}
